/*******************************************************************************
 * Copyright (c) 2008 Nels E. Beckman.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Nels E. Beckman - initial API and implementation
 *******************************************************************************/
package edu.cmu.cs.nbeckman.wptranslator;

/**
 * This element represents document text.
 * 
 * @author Nels E. Beckman
 */
public class Text implements Element {

	private final String text;
	
	public Text(String text) {
		this.text = text;
	}
	
	@Override
	public String drawAsHTML() {
		return this.text;
	}
	
	@Override
	public String toString() {
		return this.text;
	}

	@Override
	public String drawAsRTF() {
		// We've got to manually escape any RTF control characters.
		return Text.escapeRTF(this.text);
	}

	/**
	 * Escapes any '{' or '\' characters found in this text
	 * element by inserting a '\' in front.
	 */
	private static String escapeRTF(String text2) {
		String result = text2.replace("\\", "\\\\");
		result = result.replace("{", "\\{");
		result = result.replace("}", "\\}");
		return result;
	}
	
	// Just to test out this weird escape thing.
	public static void main(String[] args) {
		String text2 = "\\Totally\\Dude";
		System.out.println("Escaping \"" + text2 + "\"");
		System.out.println(escapeRTF(text2));
		
		String text3 = "\\Totally, Thi{ is i}";
		System.out.println("Escaping \"" + text3 + "\"");
		System.out.println(escapeRTF(text3));
	}
}
